const app = getApp();

// component/trading-record/index.js
Component({
	/**
	 * 组件的属性列表
	 */
	properties: {
		role: {
			type: String,
			value: "",
			// observer: function (value) {
			// 	let goods_1 = this.data.goods_1
			// 	//农户展示数据 2排
			// 	if (value == 2) {
			// 		this.setData({
			// 			goosList: goods_1
			// 		})
			// 	}

			// }
		},
		type: {
			type: String,
			value: "",
			// observer: function (value) {
			// 	let goods_1 = this.data.goods_1
			// 	if (value == 2) {
			// 		this.setData({
			// 			goosList: goods_1
			// 		})
			// 	}
			// }
		},
		userId: {
			type: String,
			value: "",
		},
		cropId: {
			type: Number,
			value: "",
		},

	},
	observers: {
		'cropId': function (value) {
			this.getData()
			console.log(value, '监听111')
		}
	},

	/**
	 * 组件的初始数据
	 */
	data: {
		searchValue: "",
		current: 1,
		goosList: [],
		isLastPage: false,
		size: 10,
		flag: 1,
		showBookPicker: false,
		bookList: [],
		orderItem: {},

	},
	lifetimes: {
		attached: function () {
			// 在组件实例进入页面节点树时执行
		},
		detached: function () {
			// 在组件实例被从页面节点树移除时执行
		},
	},
	pageLifetimes: {
		show: function () {
			// this.getData()
			this.getAccountBookEasy()
			this.onPullDownRefresh()
		},
	},

	/**
	 * 组件的方法列表
	 */
	methods: {
		// 乘
		floatMultiply(arg1, arg2) {
			if (arg1 == null || arg2 == null) {
				return null;
			}
			var n1, n2;
			var r1, r2; // 小数位数
			try {
				r1 = arg1.toString().split(".")[1].length;
			} catch (e) {
				r1 = 0;
			}
			try {
				r2 = arg2.toString().split(".")[1].length;
			} catch (e) {
				r2 = 0;
			}
			n1 = Number(arg1.toString().replace(".", ""));
			n2 = Number(arg2.toString().replace(".", ""));
			return n1 * n2 / Math.pow(10, r1 + r2);
		},
		//获取账本
		getAccountBookEasy() {
			wx.$apis.getAccountBookEasy().then(res => {
				let bookList = res.data && res.data.map(item => {
					item.text = item.name
					return item
				})
				this.setData({
					bookList: bookList
				})
			})
		},
		syncToAccountBook(e) {
			this.setData({
				orderItem: e.currentTarget.dataset.item,
				showBookPicker: true
			})
		},
		onBookCancel() {
			this.setData({
				showBookPicker: false
			})
		},
		onBookConfirm(event) {
			const {
				value
			} = event.detail;
			let params;
			if (this.data.role == 1) {
				if (this.data.flag == 1) {  //货主的
					params = {
						accountBookId: value.id,
						financeName: '卖菜',
						recordTime: wx.$dayjs().format("YYYY-MM-DD HH:mm:ss"),
						orderType: 1,
						orderId: this.data.orderItem.id,
						type: 0,
					}
				} else {
					params = {
						accountBookId: value.id,
						financeName: '买菜',
						recordTime: wx.$dayjs().format("YYYY-MM-DD HH:mm:ss"),
						orderType: 2,
						orderId: this.data.orderItem.id,
						type: 1,
					}
				}
			} else {
				params = {
					accountBookId: value.id,
					financeName: '卖菜',
					recordTime: wx.$dayjs().format("YYYY-MM-DD HH:mm:ss"),
					orderType: 2,
					orderId: this.data.orderItem.id,
					type: 2,  //收入支出类型
				}
			}
			wx.showLoading({
					title: "正在同步"
			})
			wx.$apis.addAccountBookRecord(params).then(res => {
				this.setData({
					showBookPicker: false
				})
				wx.showToast({
					title: '同步完成！',
					icon: 'success',
					duration: 1500
				})
				this.onPullDownRefresh()
			})
		},
		//分页
		getData() {
			// if (this.data.role == 2) {
			// 	this.data.goosList = [];
			// }else{
			// 	this.data.goosList = [];

			// }
			//进行中   市场调用采购单分页  小工调用备货单分页
			let {
				role,
				current,
				size,
				type,
				userId,
				flag,
				cropId

			} = this.data;
			let api, parmas;
			if (type == '1') {//市场-货主出库分页 /trade/outStock/marketer/page
				this.setData({
					flag: 1
				})
				api = "marketerPage";
				parmas = {
					companyId: app.globalData.userInfo.companyData.companyId,
					current,
					size,
					goodsOwnerUserId: userId
				}
				console.log(parmas, 'parmas')

			} else if (type == 2) { //市场-农户入库分页 /trade/enterStock/marketer/page
				this.setData({
					flag: 2
				})
				api = "getMarketList",
					parmas = {
						companyId: app.globalData.userInfo.companyData.companyId,
						current,
						size,
						farmerUserId: userId
					}
			} else if (role == 2) {  //农户-入库分页（农户） /trade/enterStock/farmer/page
				this.data.goosList = [];
				this.setData({
					flag: 2
				})
				api = "getFarmerList"
				parmas = {
					companyId: app.globalData.userInfo.companyData.companyId,
					current,
					size,
					cropId: cropId
				}
			}
			wx.showLoading({
				title: "正在请求数据中"
			})
			console.log('参数', parmas)
			wx.$apis[api](parmas).then(res => {
				wx.stopPullDownRefresh();
				let old = this.data.goosList;
				let allList = res.data && res.data.records.map(item => {
					item.transactionPrice = wx.$getYuan(item.transactionPrice)
					item.countPrice = wx.$getYuan(item.countPrice)
					item.managementFee = wx.$getYuan(item.managementFee)
					if (item.settlementTime) {
						item.settlementTime = item.settlementTime.replace("T", " ")
					}
					if (item.createTime) {
						item.createTime = item.createTime.replace("T", " ")
					}
					if (item.outStockTime) {
						item.outStockTime = item.outStockTime.replace("T", " ")
					}
					item.totle = this.floatMultiply(item.transactionPrice, item.demandWeight)
					return item
				})
				if (allList && allList.length) {
					let newList = old.concat(allList)
					this.setData({
						goosList: newList
					})
					if (allList.length < size) {
						this.setData({
							isLastPage: true
						})
					}
				} else {
					this.setData({
						isLastPage: true
					})
				}
			}).catch(err => {
				wx.hideLoading()
			})
		},

		onPullDownRefresh() {
			this.setData({
				current: 1,
				searchValue: "",
				goosList: [],
				isLastPage: false
			})
			this.getData()
		},
		onReachBottom() {
			let {
				current,
				isLastPage
			} = this.data
			if (isLastPage) {
				return
			}
			this.setData({
				current: (Number(current) + 1)
			})
			this.getData()
		},
		// onSearch() {
		// 	this.setData({
		// 		current: 1,
		// 		goodsList: [],
		// 		isLastPage: false
		// 	})
		// 	this.getData()
		// },

		//跳转详情页面
		gotodetail(e) {
			// console.log(e)
			let id = e.currentTarget.dataset.id
			if (this.data.type == 1) {
				wx.navigateTo({
					url: "/pages/dashboard/hzdetail/hzdetail?id=" + id + "&type=3"
				});
			} else {
				wx.navigateTo({
					url: "/pages/business/details/index?id=" + id
				});
			}
		},
	}
})
